﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;

namespace GameControlTest
{
    public delegate void WorkOnTransformsDelegate(Point[] points);

    public partial class GameControlBackgroundWorker : UserControl
    {
        private BackgroundWorker _bw;
        private List<CompositeTransform> _rectTransforms;
        private List<Point> _points;
        private DateTime _lastTime;

        private static Random s_random = new Random();

        public GameControlBackgroundWorker()
        {
            InitializeComponent();

            _rectTransforms = new List<CompositeTransform>();
            _points = new List<Point>();

            for (int i = 0; i < App.ObjectCount; i++)
            {
                Rectangle r = new Rectangle();
                r.Width = 40;
                r.Height = 40;
                r.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)s_random.Next(256),
                    (byte)s_random.Next(256), (byte)s_random.Next(256)));
                CompositeTransform ct = new CompositeTransform();
                ct.TranslateX = s_random.Next(800) - 400;
                ct.TranslateY = s_random.Next(600) - 300;
                _points.Add(new Point(ct.TranslateX, ct.TranslateY));
                r.RenderTransform = ct;
                _rectTransforms.Add(ct);
                this.LayoutRoot.Children.Add(r);
            }

            _lastTime = DateTime.Now;

            _bw = new BackgroundWorker();
            _bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
            _bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bw_RunWorkerCompleted);
            _bw.RunWorkerAsync();
        }

        private void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            DateTime now = DateTime.Now;
            TimeSpan elapsedTime = now - _lastTime;
            _lastTime = now;
            float elapsedTimeRatio = (float)elapsedTime.TotalSeconds / ((float)60 / 1000);

            System.Threading.Thread.Sleep(App.Sleep);
            for (int i = 0; i < _points.Count; i++)
            {
                Point p = _points[i];
                //p.X = s_random.Next(800) - 400;
                //p.Y = s_random.Next(600) - 300;
                p.X += elapsedTimeRatio;
                p.Y += elapsedTimeRatio;
                _points[i] = p;
            }
            Point[] pointCopy = new Point[_points.Count];
            _points.CopyTo(pointCopy);
            Deployment.Current.Dispatcher.BeginInvoke(new WorkOnTransformsDelegate(AdjustTransforms),
                new object[] { pointCopy });
        }

        private void _bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _bw.RunWorkerAsync();
        }

        private void AdjustTransforms(Point[] points)
        {
            for (int i = 0; i < _rectTransforms.Count; i++)
            {
                _rectTransforms[i].TranslateX = points[i].X;
                _rectTransforms[i].TranslateY = points[i].Y;
            }
        }
    }
}
